﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace With_the_blank_page
{
    public partial class Form1 : Form
    {
        public static List<Button> visButs = new List<Button>();
        public static string[] loadVars = new string [4];
        public static string txtSLTest;
        public static Button lastClickedButton;
        public Form1()
        {
            InitializeComponent();
        }

        private void txtIndexHead_Click(object sender, EventArgs e)
        {

        }

        public void Intro1()
        {   
                txtIndexHead.Text = "Вступление";
                txtIndexBody.Text = "Introduction1 placeholder";
                btnindexstart.Text = "Далее";
        }

        public void Intro2()
        {
            txtIndexHead.Text = "Вступление";
            txtIndexBody.Text = "Introduction2 placeholder neighbor select";
            btnindexstart.Text = "Далее";
            btnindexstart.Enabled = false;
            btnindexstart.Visible = false;
            F1AddBtnMNeighbor();
            F1AddBtnFNeighbor();
        }

        public void Intro3()
        {
            txtIndexHead.Text = "Вступление";
            btnindexstart.Text = "Далее";
            Button inputBTN = (Button)lastClickedButton;
            if (inputBTN.Text == "I2PHAswM")
            {
                neighborGender = "male";
            }
            if (inputBTN.Text == "I2PHAswF")
            {
                neighborGender = "female";
            }
            txtIndexBody.Text = "Placeholder for intro 3 neighbor " + neighborGender;
            foreach (Button butt in visButs)
            {
                butt.Enabled = false;
                butt.Visible = false;
            }
            btnindexstart.Visible = true;
            btnindexstart.Enabled = true;
        }

        public void Intro4()
        {
            txtIndexHead.Text = "Вступление";
            btnindexstart.Text = "Далее";
            if (neighborGender == "male")
            {
                txtIndexBody.Text = "Intro4 Placeholder neighbor - M";
            }
            if (neighborGender == "female")
            {
                txtIndexBody.Text = "Intro3 Placeholder neighbor - F";
            }
        }
        
        private void btnIndexStart_Click(object sender, EventArgs e)
        {
            lastClickedButton = (Button)sender;
            switch (currentState)
            {
                case "":
                    currentState = "intro1";
                    break;
                case "intro1":
                    currentState = "intro2";
                    break;
                case "intro2":
                    currentState = "intro3";
                    break;
                case "intro3":
                    currentState = "intro4";
                    break;
            }
            LoadCurrentState();
        }

        private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "vsf file (*.vsf)|*.vsf";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    loadVars[0] = SaveLoad.Save.ToLowCase(currentState);
                    loadVars[1] = visButs.Count.ToString();
                    foreach (Button butt in visButs)
                    {
                        loadVars[1] += SaveLoad.Save.ToLowCase(butt.Name.ToString());
                    }
                    loadVars[2] = neighborGender;
                    loadVars[3] = SaveLoad.Save.ToLowCase(lastClickedButton.Name);
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(myStream))
                    {
                        file.Write("");
                        foreach (string var in loadVars)
                        {
                            file.WriteLine(SaveLoad.Save.Encript(var));
                        }
                    }
                    myStream.Close();
                    MessageBox.Show("Данные успешно сохранены", "Success", MessageBoxButtons.OK);
                }
            }

        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "vsf file(*.vsf)|*.vsf";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (System.IO.StreamReader file = new System.IO.StreamReader(myStream))
                    {
                        string line;
                        int count = 0;
                        while ((line = file.ReadLine()) != null)
                        {
                            loadVars[count] = line;
                            count++;
                        }

                    }
                    myStream.Close();
                    currentState = SaveLoad.Load.Decript(loadVars[0]);
                   // LoadButts(SaveLoad.Load.ReturnButtons(SaveLoad.Load.Decript(loadVars[1])));
                    //foreach (Button b in visButs)
                    //{
                    //    b.Visible = true;
                    //    b.Enabled = true;
                    //}
                    neighborGender = loadVars[2];
                    lastClickedButton = (Button)StrToButton(loadVars[3]);
                    LoadCurrentState();
                }
            }
        }

        private void F1AddBtnMNeighbor()
        {
            Button intro2answm = new Button();
            intro2answm.Click += new System.EventHandler(intro2answm_Click);
            intro2answm.Text = "I2PHAswM";
            intro2answm.Location = new System.Drawing.Point(295, 405);
            intro2answm.Size = new System.Drawing.Size(100, 30);
            intro2answm.UseVisualStyleBackColor = true;
            this.Controls.Add(intro2answm);
            visButs.Add(intro2answm);
        }

        private void F1AddBtnFNeighbor()
        {
            Button intro2answf = new Button();
            intro2answf.Click += new System.EventHandler(intro2answf_Click);
            intro2answf.Text = "I2PHAswF";
            intro2answf.Location = new System.Drawing.Point(405, 405);
            intro2answf.Size = new System.Drawing.Size(100, 30);
            intro2answf.UseVisualStyleBackColor = true;
            this.Controls.Add(intro2answf);
            visButs.Add(intro2answf);
            
        }

        private void intro2answm_Click(object sender, EventArgs e)
        {
            lastClickedButton = (Button)sender;
            currentState = "intro3";
            LoadCurrentState();
        }

        private void intro2answf_Click(object sender, EventArgs e)
        {
            lastClickedButton = (Button)sender;
            currentState = "intro3";
            LoadCurrentState();
        }

        public void LoadCurrentState()
        {
            string invokingFunc = "";
            char makeItUpper = currentState[0];
            makeItUpper = Char.ToUpper(makeItUpper);
            invokingFunc = makeItUpper + (currentState.Substring(1, currentState.Length - 1));
            MethodInfo mi = this.GetType().GetMethod(invokingFunc);
            mi.Invoke(this, null);
        }

        public object StrToButton(string name)
        {
            var arr = this.Controls.Find(name, true);
            return arr;
        }

        public void LoadButts(string[] names) 
        {

            for (int c = 0; c < names.Length; c++)
            {
                Button b = (Button)(StrToButton(names[c]));
                visButs.Add(b);
            }
        }
    }
}
